from jili.core.printlog import print
import psutil
def get_pid(l):
    s={}
    for proc in psutil.process_iter():
        p = proc.pid
        try:
            pp = proc
            i = pp.cmdline()
            s[p] = i
        except psutil.AccessDenied as e:
            pass
        except Exception as e:
            print("err1", p, e)
    rst={}
    for i in l:
        for pid,ss in s.items():
            if isinstance(i,list):
                isr=True
                for ii in i:
                    if ii not in ss:
                        isr=False
                if isr:
                    rst[pid] = ss
            else:
                if i in ss:
                    rst[pid]=ss
                # for ii in ss:
                #     if i in ii:
                #         rst[pid] = ss
    return rst
def isrun(l):
    rst=False
    for proc in psutil.process_iter():
        p = proc.pid
        try:
            pp = proc
            i = pp.cmdline()
            rst= l in i
        except psutil.AccessDenied as e:
            pass
        except Exception as e:
            print("err1", p, e)
    return rst
def isruns(ll):
    rst=[False]*len(ll)
    for proc in psutil.process_iter():
        p = proc.pid
        try:
            pp = proc
            i = pp.cmdline()
            n=0
            for l in ll:
                rst0= l in i
                if rst0:
                    rst[n]=True
                n=n+1
        except psutil.AccessDenied as e:
            pass
        except Exception as e:
            print("err1", p, e)
    return rst
